<?php
	require_once('config.php');
	
	function GetNumNewsSearch($keyword='', $status=null)
	{
		if($keyword == '')
		{
			if($status == null)
				$sql = "SELECT count(news_id) as num FROM `news_tb`
						WHERE `news_status` <= 2";
			else
				$sql = "SELECT count(news_id) as num FROM `news_tb` WHERE `news_status` = $status";
		}					
		else
		{
			if($status == NULL)
				$sql = "SELECT count(news_id) as num FROM `news_tb`
						WHERE 	`news_status` <= 2 AND
								`news_tb`.`news_title` like '%$keyword%'";
			else
				$sql = "SELECT count(news_id) as num FROM `news_tb`
						WHERE 	`news_tb`.`news_title` like '%$keyword%' AND `news_status` = $status";
		}
			
			
		
		$sql = mysql_query($sql, dbconnect());
		$result = mysql_fetch_assoc($sql);
		return $result['num'];
	}
	function GetNewsTicker()
	{
		$sql = "SELECT * FROM `news_tb`
				WHERE `news_status` = ".NEWS_STATUS_NEW. " ORDER BY `news_order` DESC, `news_date` DESC";
					
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($news_row = mysql_fetch_assoc($sql)) { 
			$result[] = $news_row;
		} 
		return $result; 
	}
	
	function GetNewsSearch($curPage, $rowPerPage, $keyword='', $status=null)
	{
		if($curPage > 0)
			$curPage--;
		$from = $curPage * $rowPerPage;
		
		
		if($keyword == '')
		{
			if($status == null)	
				$sql = "SELECT *  FROM `news_tb` 
						WHERE `news_status` <= 2
						ORDER BY `news_status` DESC, `news_order` DESC, `news_date` DESC
						LIMIT $from, $rowPerPage ";
			else
				$sql = "SELECT *  FROM `news_tb` 
						WHERE `news_status` = $status
						ORDER BY `news_order` DESC, `news_date` DESC
						LIMIT $from, $rowPerPage ";
		}
		else
		{
			if($status == null)
				$sql = "SELECT *  FROM `news_tb`
						WHERE	`news_status` <= 2 AND
								`news_tb`.`news_title` like '%$keyword%' 
						ORDER BY `news_status` DESC, `news_order` DESC, `news_date` DESC
						LIMIT $from, $rowPerPage";
			else
				$sql = "SELECT *  FROM `news_tb`
						WHERE 	`news_tb`.`news_title` like '%$keyword%' AND		
								`news_status` = $status
						ORDER BY `news_order` DESC, `news_date` DESC
						LIMIT $from, $rowPerPage";
		}
								
		//var_dump($sql); die();
		$sql = mysql_query($sql, dbconnect());
		
		$result = array(); 
		while ($news_row = mysql_fetch_assoc($sql)) { 
			$result[] = $news_row;
		} 
		return $result; 
	}
	function InsertNews($arr, $status=1)
	{
		$arr['news_content'] = addslashes($arr['news_content'] );
		$arr['news_description'] = addslashes($arr['news_description'] );
		$arr['news_title'] = addslashes($arr['news_title'] );
		
		$sql = "INSERT INTO `news_tb` 
					(	`news_title`,`news_img`,`news_description`, `news_content`, `news_status`) 
				VALUE ('{$arr['news_title']}', '{$arr['url1']}','{$arr['news_description']}', '{$arr['news_content']}', $status )";
		
		
		return mysql_query($sql, dbconnect());
	}
	
	function GetNewsById($id)
	{
		$sql = mysql_query("SELECT * FROM `news_tb`
							WHERE 	`news_tb`.`news_id` = $id", dbconnect());
		$result = mysql_fetch_assoc($sql);
		if($result)
		{
			$result['news_description'] = stripcslashes($result['news_description']);
			$result['news_content'] = stripcslashes($result['news_content']);
			$result['news_title'] = stripcslashes($result['news_title']);
		}
		return $result;
		
	}
	function UpdateNews($news_id, $arr)
	{
		
		$arr['news_content'] = addslashes($arr['news_content'] );
		$arr['news_description'] = addslashes($arr['news_description'] );
		$arr['news_title'] = addslashes($arr['news_title'] );
		
		$sql = "UPDATE `news_tb` 
						SET `news_title`='{$arr['news_title']}', 
							`news_img`   = '{$arr['url1']}',
							`news_description`='{$arr['news_description']}', 
							`news_content`='{$arr['news_content']}'
						WHERE `news_id`=$news_id";
		
		mysql_query($sql, dbconnect());
	}
	function DeleteNews($news_id){
		return mysql_query("DELETE FROM `news_tb` WHERE `news_id`= $news_id", dbconnect());
	}
	function UpdateNewsNew($news_id, $adj)
	{
		if ($adj == 'new')
			$sql = "UPDATE `news_tb` 
							SET `news_status`= 2 
							WHERE `news_id`=$news_id";
		else
			$sql = "UPDATE `news_tb` 
							SET `news_status`= 1
							WHERE `news_id`=$news_id";
		return mysql_query($sql, dbconnect());
	}
	function UpdateNewsOrder($news_id, $adj)
	{
		if ($adj == '+')
			$sql = "UPDATE `news_tb` 
							SET `news_order`=`news_order` + 1 
							WHERE `news_id`=$news_id";
		else
			$sql = "UPDATE `news_tb` 
							SET `news_order`=`news_order` - 1 
							WHERE `news_id`=$news_id";
		return mysql_query($sql, dbconnect());
	}
?>